<?php

namespace App\Http\Controllers\Admin\order;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;

class IndexController extends Controller
{
    //订单列表
    public function index(Request $request)
    {
    	//遍历
    	//搜索关键字
    	$data = DB::table('orders as c1')
    		->leftJoin('users_detail as c2', 'c1.member_id', '=', 'c2.uid')
    		->leftJoin('goods as c3', 'c1.goods_id', '=', 'c3.id')
    		->where('post_number', 'like', '%'.$request -> input('keywords').'%')
    		->select('c1.*', 'c2.nickname', 'c2.phone', 'c3.goods_price')
    		->orderBy('c1.id')
    		->get();

    	return view('admin.order.index', ['title' => '订单列表', 'data' => $data, 'request' => $request -> all()]);
	}

	//订单详情
	public function orderdetail($member_id, $goods_id)
	{

		$data = DB::select('SELECT orders.*,users_detail.nickname,users_detail.phone,address.*,goods.goods_price,goods.goods_name,goods.goods_price FROM orders,users_detail,address,goods WHERE orders.member_id=users_detail.uid AND orders.goods_id=goods.id AND orders.member_id=address.uid AND orders.member_id=:member_id AND orders.goods_id=:goods_id',['member_id' => $member_id, 'goods_id' => $goods_id]);

		return view('admin.order.orderdetail', ['title' => '订单详情', 'data' => $data]);
	}

	//订单分类
   
    public function ajaxPay(Request $request)
    {
        $status = $request -> status;


        if($status == 5)
        {
            $data = DB::table('orders as c1')
                ->leftJoin('users_detail as c2', 'c1.member_id', '=', 'c2.uid')
                ->leftJoin('goods as c3', 'c1.goods_id', '=', 'c3.id')
                ->where('post_number', 'like', '%'.$request -> input('keywords').'%')
                ->select('c1.*', 'c2.nickname', 'c2.phone', 'c3.goods_price')
                ->orderBy('c1.id')
                ->get();
        }
        else
        {
            // $data = DB::select('SELECT orders.*,users_detail.nickname,users_detail.phone FROM orders,users_detail WHERE orders.member_id=users_detail.uid AND orders.post_status=:status', ['status' => $status]);

            $data = DB::table('orders')
                -> leftJoin('users_detail', 'orders.member_id', '=', 'users_detail.uid')
                -> select('orders.*', 'users_detail.nickname', 'users_detail.phone')
                -> where('orders.post_status', '=',  $status)
                ->get(5);
        }
    
        return $data;


    }


	public function ajaxOrder(Request $request)
    {
        $status = $request -> status;

        if($status == 5)
        {
	    	$data = DB::table('orders as c1')
	    		->leftJoin('users_detail as c2', 'c1.member_id', '=', 'c2.uid')
	    		->leftJoin('goods as c3', 'c1.goods_id', '=', 'c3.id')
	    		->where('post_number', 'like', '%'.$request -> input('keywords').'%')
	    		->select('c1.*', 'c2.nickname', 'c2.phone', 'c3.goods_price')
	    		->orderBy('c1.id')
	    		->get();
        }
        else
        {
        	$data = DB::select('SELECT orders.*,users_detail.nickname,users_detail.phone FROM orders,users_detail WHERE orders.member_id=users_detail.uid AND orders.post_status=:status', ['status' => $status]);
        }
    
        return $data;

    }

    //发货
    public function ajaxSend(Request $request)
    {
        $id = $request -> id;
        
        $status = DB::table('orders') -> where('id', $id) -> first() -> post_status;

        if($status == 1)
        {
            $data = DB::table('orders') -> where('id', $id) -> update(['post_status' => 2]);
            if($data)
            {
                return 2;
            }else
            {
                return 0;
            }  
            
        }

        if($status == 6)
        {
            $res = DB::table('orders') -> where('id', $id) -> update(['post_status' => 7]);
            if($res)
            {
                return 7;
            }else
            {
                return 0;
            }
        }

    }

}
